<?php

/**
 * @author juanma
 * @copyright 2012
 */


class Sgit_Graficos_Salas extends Sgit_Graficos_Parent
{


    private function getData()
    {

        $m_InformeElectricidadMensualSalas = $this->loadModel('InformeElectricidadMensualSalas');
        $m_sala = $this->loadModel('Salas');

        $this->key = $this->request->getParam('key', 'potencia');
        $this->id = $this->request->getParam('id');


        switch ($this->key)
        {
            case 'consumo':
                $this->y = 'kWh';
                $this->yaxistitle = 'kWh';
                $this->subtitle = "Consumo kWh";
                $this->format = '%d';
                break;
            case 'potencia':
                $this->y = 'kW';
                $this->yaxistitle = 'kW';
                $this->subtitle = "Potencia kW";
                $this->format = '%d';
                break;
            case 'ratiowm2':
                $this->y = 'W/m2';
                $this->yaxistitle = 'W/m2';
                $this->subtitle = "Ratio W/m2";
                $this->format = '%d';
                break;

        }


        $filas = $m_InformeElectricidadMensualSalas->getAll(array('id_sala=?' => $this->id), array('anio DESC', 'mes desc'), null, $this->limit);
        $filas = array_reverse($filas);

        $sala = $m_sala->getRow($this->id);


        $this->title = 'Sala: ' . $sala['nombre'] . ' ' . Sgit_Cache::single()->model('Datacenter')->getKey($filas[0]['id_zona']);
        $this->subtitle = $this->subtitle . ' Superfice Neta:' . $this->view->numES($sala['sup_neta'], 2) . ' m2';


        $i = 0;
        $fecha = new Zend_Date();
		$fecha->setDay(1);
        foreach ($filas as $v)
        {

            $fecha->setMonth($v['mes'])->setYear($v['anio']);

            $v['potencia'] = Sgit_Util_Potencia::run($v['consumo'], $fecha);
            $v['ratiowm2'] = $v['potencia'] * 1000 / $sala['sup_neta'];

            $this->label[$i]=ucfirst($fecha->get('MMM-yy'));
            $this->labelJsonFormat[$i]=ucfirst($fecha->get('MMM-yyyy'));
            $this->labelJson[$i]=$fecha->get('yyyy-MM-dd');
            
            $this->value['potencia'][$i] =$this->setValue($v['potencia']);
            $this->value['consumo'][$i] = $this->setValue($v['consumo']);
            $this->value['ratiowm2'][$i] = $this->setValue($v['ratiowm2']);

            $i++;
        }
        
        
    }


    public function jpGrapgh()
    {

        $this->getData();
    



        $this->loadJpgraph('bar');
        $this->loadJpgraph('line');


        $step = $this->_step(count($this->value[$this->key]), $this->width);
        $nombre = md5($this->key . $this->width . $this->height . serialize($this->value[$this->key]));


        $graph = $this->_graph($this->width, $this->height);
        $graph->SetMargin(75, 75, 30, 80);

        $graph->title->Set(utf8_decode($this->title));
        $graph->subtitle->Set(utf8_decode($this->subtitle));
        $graph->yaxis->SetTitle($this->yaxistitle, 'middle');
        $graph->xaxis->SetTickLabels($this->label);
        $graph->xaxis->SetTextLabelInterval($step);
        $graph->xaxis->SetLabelAngle(90);


        $barplot = $this->_barPlot($this->value[$this->key], '#c0c0c0');
        $barplot->value->Show();
        $barplot->value->SetFont(FF_ARIAL, FS_BOLD, 8);
        $barplot->value->SetAngle(90);
        $barplot->value->SetFormat($this->format);
        $barplot->SetValuePos('center');
        $barplot->SetFillGradient( $this->color . '@0.3',  $this->color . '@0.6', GRAD_VERT);
        $barplot->SetWidth(1);
        $barplot->SetValuePos('center');

        $graph->Add($barplot);




        $graph->footer->left->Set(utf8_decode("Fuente: Suma de consumos de clientes en sala"));
        $graph->Stroke();


    }

    public function googleVisualization(&$view)
    {

        $this->limit = null;
        $this->getData();

        $view->title = $this->title . ' ' . $this->subtitle;
        $view->subtitle = $this->subtitle;
        $view->options = "
                'title': '{$view->title}',
                'hAxis': {'slantedText': true,slantedTextAngle:90},
                'legend': {'position': 'right'},
                'animation' : {'duration' : 2000},
                'curveType' : 'function',
                'vAxis' : {'title': 'pue'},
                 strictFirstColumnType: false,
                'vAxis' : {'minValue': 0, title: '$this->yaxistitle'},
                 colors:['green'],
        ";
        $view->y = $this->y;
        $view->label = $this->labelJson;
        $view->label_format = $this->labelJsonFormat;
        $view->data = $this->value[$this->key];


    }


}
